/**
 * Created by wei on 17/11/23.
 */


import React, { Component } from 'react';
import axios from 'axios';
import base from '../base.js';
import Information from './information';
import cookie from '../cookie.js';
import DataTables from './datatables';
import './person.css'
import Foot from '../front/foot';

const basePath = base().basePath;

//获取url参数
function GetQueryString (name) {
    var after = window.location.hash.split("?")[1];
    if (after) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = after.match(reg);
        if (r != null) {
            return decodeURIComponent(r[2]);
        }
        else {
            return null;
        }
    }
}

class Content extends  Component{

    constructor(props){
        super(props);

        var token = GetQueryString("t");
        if(null == token){
            token = cookie.getCookie("token");
        }
        console.log(token);
        this.state = {
            token: token,
            user:{},
            curTab:'follow',
            dataSource:[],
            delId:void(0),
            pageInfo: {
                pageNumber:0,
                pageSize:10,
                totalPages:0,
                totalElements:0,
                last:true,
                first:false,
                size:0,

            },
        }
    }


    componentDidMount(){
        axios.get(basePath+"/user/find", {headers: { token: this.state.token }
        }).then(
            (res) => {
                console.log(JSON.stringify(res));
                this.setState({
                    user: res.data,
                });
            }
        );

        this.showContent(this.state.curTab);
    }


    showContent(v){

        switch (v){
            case 'follow':
                this.getData(basePath+'/topic/follow', v);
                break;
            case 'topic':
                this.getData(basePath+'/topic/t', v);
                break;
            case 'replay':
                this.getData(basePath+'/replay/r', v);
                break;
            case 'comment':
                this.getData(basePath+'/comment/c', v);
                break;

        }
    }



    getData(url, v){
        this.state.pageInfo.pageNumber=0;
        axios.post(url , this.state.pageInfo, {headers: { token: this.state.token }
        })
            .then((res) => {
                if(res.status == 200){
                    var pageInfo = {};
                    var dataSource = res.data.content;
                    pageInfo.totalPages = res.data.totalPages;
                    pageInfo.totalElements = res.data.totalElements;
                    pageInfo.last = res.data.last;
                    pageInfo.first = res.data.first;
                    pageInfo.size = res.data.size;
                    pageInfo.pageSize = res.data.size;
                    pageInfo.pageNumber = res.data.number;
                    this.setState({
                        dataSource: dataSource,
                        pageInfo: pageInfo,
                        curTab: v,
                    });
                }
            });
    }



    contentTab(){


        switch (this.state.curTab){
            case 'follow':
                return <DataTables token={this.state.token} type="follow" dataSource={this.state.dataSource} pageInfo={this.state.pageInfo} />;
                break;
            case 'topic':
                return <DataTables token={this.state.token} type="topic" dataSource={this.state.dataSource} pageInfo={this.state.pageInfo} />;
                break;
            case 'replay':
                return <DataTables token={this.state.token} type="replay" dataSource={this.state.dataSource} pageInfo={this.state.pageInfo} />;;
                break;
            case 'comment':
                return <DataTables token={this.state.token} type="comment" dataSource={this.state.dataSource} pageInfo={this.state.pageInfo} />;;
                break;

        }
    }


    render(){

        return (
            <div className="content mid wi90" >
                <div style={{marginTop:'30px'}}>
                    <div>
                        <Information user={this.state.user} token={this.state.token}/>
                    </div>
                    <div style={{marginTop:'10px'}}>
                        <div className="border1px" style={{padding:'10px 10px',margin: '5px 5px'}}>
                            <div style={{width:'100%', overflow:'hidden'}}>
                                <div className="float_l borderBh" style={{width:'210px',height:'41px'}}>&nbsp;</div>
                                <div className={this.state.curTab == 'follow' ? 'float_l border1px borderBn' : 'float_l border1px'} style={{width:'100px', height:'40PX',lineHeight:'40px'}}>
                                    <span style={{marginLeft:'10px'}}>
                                        <a onClick={this.showContent.bind(this,'follow')} className="btn2" href="javascript:;">我的关注</a>
                                    </span>
                                </div>
                                <div className="float_l borderBh" style={{width:'10px',height:'41px'}}>&nbsp;</div>
                                <div className={this.state.curTab == 'topic' ? 'float_l border1px borderBn' : 'float_l border1px'} style={{width:'100px', height:'40PX',lineHeight:'40px'}}>
                                    <span style={{marginLeft:'10px'}}>
                                        <a onClick={this.showContent.bind(this,'topic')} className="btn2" href="javascript:;">我的发表</a>
                                    </span>
                                </div>
                                <div className="float_l borderBh" style={{width:'10px',height:'41px'}}>&nbsp;</div>
                                <div className={this.state.curTab == 'replay' ? 'float_l border1px borderBn' : 'float_l border1px'} style={{width:'100px', height:'40PX',lineHeight:'40px'}}>
                                    <span style={{marginLeft:'10px'}}>
                                        <a  onClick={this.showContent.bind(this,'replay')} className="btn2" href="javascript:;">我的回复</a>
                                    </span>
                                </div>
                                <div className="float_l borderBh" style={{width:'10px',height:'41px'}}>&nbsp;</div>
                                <div className={this.state.curTab == 'comment' ? 'float_l border1px borderBn' : 'float_l border1px'} style={{width:'100px', height:'40PX',lineHeight:'40px'}}>
                                    <span style={{marginLeft:'10px'}}>
                                        <a  onClick={this.showContent.bind(this,'comment')} className="btn2" href="javascript:;">我的评论</a>
                                    </span>
                                </div>
                                <div className="float_l borderBh" style={{width:'350px',height:'41px'}}>&nbsp;</div>
                            </div>
                            <div style={{marginTop:'10px'}}>
                                {this.contentTab()}
                            </div>
                        </div>
                    </div>
                </div>
                <div>
                    <Foot/>
                </div>
            </div>
        );
    }

}


export default Content;
